// $Id: bool.hpp 2 2009-09-02 08:47:42Z joseph2002 $
/*
 * Copyright 2009 Joseph Wu
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
 *
 * For more information, see http://xpcomplus.sourceforge.net
 */

#ifndef XPCOM_PROXY_BOOL_HPP
#define XPCOM_PROXY_BOOL_HPP

#include <xpcom/config.hpp>
#include <boost/noncopyable.hpp>
#include <xpcom/types.hpp>

#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_PREFIX
#endif

namespace xpcom {
namespace proxy {

//
// XPCOM proxy argument translators.
//
// bool_in(bool) --> bool_t
// bool_out(bool&) --> bool_t*
// bool_inout(bool&) --> bool_t*
//

XPCOM_FORCEINLINE
bool_t bool_in(bool b)
{
    return b ? true_ : false_;
}

class bool_out : boost::noncopyable
{
public:
    explicit bool_out(bool& b) :
        b_(b), val_(false_)
    {
    }

    ~bool_out()
    {
        b_ = val_ != false_;
    }

    XPCOM_FORCEINLINE
    operator bool_t*()
    {
        return &val_;
    }

private:
    bool& b_;
    bool_t val_;
};

class bool_inout : boost::noncopyable
{
public:
    explicit bool_inout(bool& b) :
        b_(b), val_(b ? true_ : false_)
    {
    }

    ~bool_inout()
    {
        b_ = val_ != false_;
    }

    XPCOM_FORCEINLINE
    operator bool_t*()
    {
        return &val_;
    }

private:
    bool& b_;
    bool_t val_;
};

} // namespace xpcom::proxy
} // namespace xpcom

#ifdef BOOST_HAS_ABI_HEADERS
#include BOOST_ABI_SUFFIX
#endif

#endif // XPCOM_PROXY_BOOL_HPP
